package com.sxf.biz.subjects.service;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxf.auth.enums.CommonStatusEnum;
import com.sxf.auth.login.model.AuthUser;
import com.sxf.auth.login.service.UserService;
import com.sxf.biz.customer.dao.SettleMapper;
import com.sxf.biz.model.Settle;
import com.sxf.biz.model.SubjectsBankCard;
import com.sxf.biz.subjects.dao.SubjectsBankCardMapper;
import com.sxf.biz.subjects.dao.SubjectsMapper;
import com.sxf.biz.subjects.vo.*;
import com.sxf.common.exceptions.BusinessException;
import com.sxf.common.param.PageBaseVO;
import com.sxf.consts.YtConsts;
import com.sxf.core.aspect.UserThreadLocal;
import com.sxf.core.db.page.Pagination;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * @description: TODO
 * @Author Leo
 * @date 2024/3/2620:33
 */
@Service
public class SubjectService {
    private static final Logger logger = LoggerFactory.getLogger(SubjectService.class);

    @Autowired
    SubjectsBankCardMapper subjectsBankCardMapper;
    @Autowired
    UserService userService;
    @Autowired
    SubjectsMapper subjectsMapper;
    @Autowired
    SettleMapper settleMapper;

    public Boolean updateBankInfo(UpdateBankInfoReq req){
        QueryWrapper<SubjectsBankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SubjectsBankCard::getPkUser,req.getPkUser());
        SubjectsBankCard subjectsBankCard = subjectsBankCardMapper.selectOne(queryWrapper);
        if(subjectsBankCard != null){
            //更新
            subjectsBankCard.setAccountNo(req.getAccountNo());
            subjectsBankCard.setAccountBranch(req.getAccountBranch());
            subjectsBankCard.setCardUrl(req.getCardUrl());
            subjectsBankCard.setModifier(req.getUserId());
            subjectsBankCard.setModifierName(req.getCurName());
            subjectsBankCard.setModifiedTime(new Date());
            subjectsBankCardMapper.updateById(subjectsBankCard);
        }else{
            //新增
            subjectsBankCard = new SubjectsBankCard();
            subjectsBankCard.setAccountNo(req.getAccountNo());
            subjectsBankCard.setAccountBranch(req.getAccountBranch());
            subjectsBankCard.setCardUrl(req.getCardUrl());
            subjectsBankCard.setCreator(req.getUserId());
            subjectsBankCard.setCreatorName(req.getCurName());
            subjectsBankCard.setCreatedTime(new Date());
            subjectsBankCard.setPkUser(req.getPkUser());
            subjectsBankCard.setStatus(CommonStatusEnum.VALID.getCode());
            subjectsBankCardMapper.insert(subjectsBankCard);
        }
        return true;
    }

    public UserInfoValidationRes validUserInfo(){
        UserInfoValidationRes res = new UserInfoValidationRes();
        String userId = UserThreadLocal.getUserId();
        AuthUser user = userService.getById(userId);
        if(user.getIdNo() == null || user.getUserName() ==  null){
            res.setIdNoValid(YtConsts.N);
        }else{
            res.setIdNoValid(YtConsts.Y);
        }
        QueryWrapper<SubjectsBankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SubjectsBankCard::getPkUser,userId);
        SubjectsBankCard subjectsBankCard = subjectsBankCardMapper.selectOne(queryWrapper);
        if(subjectsBankCard == null
                || subjectsBankCard.getCardUrl() == null
                || subjectsBankCard.getAccountNo() == null){
            res.setBankCardValid(YtConsts.N);
        }else{
            res.setBankCardValid(YtConsts.Y);
        }
        return res;
    }

    public UserInfoDetail getUserDetail() {
        String userId = UserThreadLocal.getUserId();
        AuthUser userInfo = userService.getById(userId);

        UserInfoDetail res = new UserInfoDetail();
        res.setIdNo(userInfo.getIdNo());
        res.setUserMobile(userInfo.getUserMobile());
        res.setUserName(userInfo.getUserName());
        res.setPkUser(userInfo.getPkUser());
        res.setSex(userInfo.getSex());
        SubjectsBankCard subjectsBankCard = subjectsBankCardMapper
                .selectOne(new QueryWrapper<SubjectsBankCard>().lambda().eq(SubjectsBankCard::getPkUser, userId));
        if(subjectsBankCard != null){
            res.setAccountNo(subjectsBankCard.getAccountNo());
            res.setAccountBranch(subjectsBankCard.getAccountBranch());
            res.setCardUrl(subjectsBankCard.getCardUrl());
        }
        return res;
    }

    public Pagination<SubProjectList> getProjectsByPage(SubProjectListReq req) {
        return subjectsMapper.getProjectList4Sub(new Pagination<>(req),req);
    }

    public List<SubSettleListInProjectRes> getSettleListByProject(SubSettleListInProjectReq req) {
        List<SubSettleListInProjectRes> res = settleMapper.getSettleList4Sub(req);
        return res;
    }
}
